#' the path to store the models
#'
#' This function can set the path of storing models.
#' @param avm.model.store.path The path of saving models. Default is './models.avm'
#' @export
model.store.path<-function(avm.model.store.path='./models.avm'){
    # if(is.null(getOption('avm.model.store.path'))) {
    #
    # }else{
    #
    # }
    options(avm.model.store.path = avm.model.store.path)
    if(!dir.exists(avm.model.store.path)){
        dir.create(avm.model.store.path)
    }
    return(getOption('avm.model.store.path'))
}

avm.model.fit_store<-function(yearmonth='2017-7',
                         avm.model.store.path='./models.avm',
                         con=NULL,
                         proptype='11',
                         dependentY = 'rentprice',
                         tabln.vec=NULL,
                         poi.data.list=NULL,
                         remove.dup = T
){
    if(is.null(con)){
        con<-dbconnect()
    }
    if(is.null(tabln.vec)){
        tabln.vec<-loadData2(con=con,proptype=proptype,yearmonth = yearmonth,remove.dup = remove.dup)
    }
    if(is.null(poi.data.list)){
        poi.data.list<-getpoi.ras(con=con,tabln.vec = tabln.vec)
    }
    killDbConnections()
    tabln.vec$ha_info.sp<-poi.data.list$ha_info.sp
    data.sets.all<-avm.data.train(tabln.vec,proptype = proptype)
    avm.models<-avm.fit(data.train = data.sets.all,dependentY =dependentY,ntree = 500,nround = 1000)
    avm.model.store(avm.models=avm.models,
                    yearmonth = yearmonth,
                    avm.model.store.path =avm.model.store.path,
                    proptype = proptype,
                    dependentY = dependentY)
}
#' store models as files
#'
#' This function can save model as files.
#' @param city_code code of city,required.
#' @param avm.models object of models,generated by \code{\link{avm.fit}}
#' @param yearmonth characters indicting year and month. The default value is '2017-7'
#' @param avm.model.store.path The path for saving models
#' @param proptype type code of properties.The default values is 11 indicting housing.
#' @param dependentY dependent variable. The default is 'saleprice'
#' @export
avm.model.store<-function(
    city_code=NULL,
    avm.models=NULL,
    yearmonth='2017-7',
    avm.model.store.path='./models.avm',
    proptype='11',
    dependentY = 'saleprice'){
    if(is.null(city_code)){
        stop('city_code must not be NULL!!!!')
    }
    if(is.null(avm.models)){
        stop('avm.model can not be NULL!!!')
    }
    if(is.null(getOption('avm.model.store.path')))        {
        model.store.path(avm.model.store.path)
    }
    filename<-paste(city_code,dependentY,proptype,yearmonth,sep='_')
    filepath<-file.path(getOption('avm.model.store.path'),filename)
    saveRDS(avm.models,file = filepath)
    cat('models saved as ',filepath,'\n')
}
#' restore models from files
#'
#' This function can read models from files
#' @param city_code code of city,required.
#' @param yearmonth characters indicting year and month. The default value is '2017-7'
#' @param avm.model.store.path The path for saving models
#' @param proptype type code of properties.The default values is 11 indicting housing.
#' @param dependentY dependent variable. The default is 'saleprice'
#' @return model object
#' @export
avm.model.restore<-function(
    city_code=NULL,
    yearmonth='2017-7',
    avm.model.store.path='./models.avm',
    proptype='11',
    dependentY = 'saleprice'){
    if(is.null(city_code)){
        stop('city_code must not be NULL!!!!')
    }
    filename<-paste(city_code,dependentY,proptype,yearmonth,sep='_')
    filepath<-file.path(getOption('avm.model.store.path'),filename)
    avm.models<-readRDS(file = filepath)
    cat('models loaded as ',filepath,'\n')
    avm.models
}
